package com.wuliu.web.controller.order;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wuliu.common.annotation.RepeatSubmit;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.system.domain.dto.CreateOrderDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wuliu.common.annotation.Log;
import com.wuliu.common.core.controller.BaseController;
import com.wuliu.common.core.domain.AjaxResult;
import com.wuliu.common.enums.BusinessType;
import com.wuliu.system.domain.TspOrder;
import com.wuliu.system.service.ITspOrderService;
import com.wuliu.common.utils.poi.ExcelUtil;
import com.wuliu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 运单Controller
 *
 * @author realue
 * @date 2023-10-25
 */
@Api(tags = {"运单管理控制器"})
@RestController
@RequestMapping("/order/order")
public class TspOrderController extends BaseController {
    @Resource
    private ITspOrderService tspOrderService;

    /**
     * 查询运单列表
     */
    @ApiOperation(value = "查询")
    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(TspOrder tspOrder) {
        tspOrder.setSysAccount(getSysAccount());
        startPage();
        List<TspOrder> list = tspOrderService.selectTspOrderList(tspOrder);
        return getDataTable(list);
    }

    /**
     * 导出运单列表
     */
    @ApiOperation(value = "导出")
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "运单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TspOrder tspOrder) {
        tspOrder.setSysAccount(getSysAccount());
        List<TspOrder> list = tspOrderService.selectTspOrderList(tspOrder);
        ExcelUtil<TspOrder> util = new ExcelUtil<TspOrder>(TspOrder.class);
        util.exportExcel(response, list, "运单数据");
    }

    /**
     * 获取运单详细信息
     */
    @ApiOperation(value = "详细")
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tspOrderService.selectTspOrderById(id));
    }

    /**
     * 新增运单
     */
    @ApiOperation(value = "新增")
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "运单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TspOrder tspOrder) {
        tspOrder.setCreateBy(getUsername());
        return toAjax(tspOrderService.insertTspOrder(tspOrder));
    }

    /**
     * 修改运单
     */
    @ApiOperation(value = "编辑")
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "运单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TspOrder tspOrder) {
        tspOrder.setUpdateBy(getUsername());
        return toAjax(tspOrderService.updateTspOrder(tspOrder));
    }

    /**
     * 删除运单
     */
    @ApiOperation(value = "删除")
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "运单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tspOrderService.deleteTspOrderByIds(ids));
    }


    @RepeatSubmit
    @ApiOperation(value = "创建运单")
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "运单", businessType = BusinessType.INSERT)
    @PostMapping("/createOrder")
    public ApiResult<Integer> createOrder(@RequestBody @Validated CreateOrderDto dto) {
        return ApiResult.success(tspOrderService.createOrder(dto));
    }

    /**
     * 导出运单列表
     */
    @ApiOperation(value = "导入运单")
    @PreAuthorize("@ss.hasPermi('order:order:importOrder')")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query")
    @PostMapping("/importOrder")
    public ApiResult<String> importOrder(@RequestPart("file") MultipartFile file) {
        try {
            ExcelUtil<TspOrder> util = new ExcelUtil<TspOrder>(TspOrder.class);
            List<TspOrder> orders = util.importExcel(file.getInputStream());
            return ApiResult.success(tspOrderService.importOrder(orders));
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
    }
}
